$(function(){
    obj.init();
});

var obj = new JsFun();
function JsFun() {
    'use strict';
    return {
        groupId: null,
        inDataGrid: null,
        outDataGrid: null,
        dom: {
            btnIn: null,
            btnOut: null,
            btnClose: null,
        },
        init: function () {
            this.groupId     = xhTool.getQuery('groupId');
            this.dom.btnIn   = $('#bt_in');
            this.dom.btnOut  = $('#bt_out');
            this.dom.btnClose= window.top.xhTop.getModalCloseDom();
            this.inDataGrid  = new xhDataGrid($('#list_in'), 'Schoolgroup/getGroupUser', {groupId: this.groupId});
            this.outDataGrid = new xhDataGrid($('#list_out'), 'Schoolgroup/getGroupUser/true', {groupId: this.groupId});

            this.inDataGrid.init().activeButton(['bt_out'], [], 'bs');
            this.outDataGrid.parseToolbar().init().activeButton(['bt_in'], [], 'bs');

            xhTool.bindDocKeyDown($('#bt_list_out_search'));
            this.listener();
        },
        listener: function () {
            var $this = this;
            //关联
            $this.dom.btnIn.click(function () {
                $this.funObj().createGroupUser();
            });
            //取消关联
            $this.dom.btnOut.click(function () {
                $this.funObj().delGroupUser();
            });
            //关闭时刷新页面
            $this.dom.btnClose.click(function () {
                $this.funObj().reloadGroup();
            });
            //未关联列表查询
            $('#bt_list_out_search').click(function () {
                $this.funObj().searchOutUser();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                createGroupUser: function () {
                    var userIds = $this.outDataGrid.getSelectValues('iUserId');
                    xhExtra.ajaxRequest('Schoolgroup/modifyGroupUser', {groupId: $this.groupId, userId: userIds}, {}, function(result) {
                        $this.inDataGrid.reload();
                        $this.outDataGrid.reload();
                    });
                },
                delGroupUser: function () {
                    var userIds = $this.inDataGrid.getSelectValues('iUserId');
                    xhExtra.ajaxRequest('Schoolgroup/modifyGroupUser/true', {groupId: $this.groupId, userId: userIds}, {}, function(result) {
                        $this.inDataGrid.reload();
                        $this.outDataGrid.reload();
                    });
                },
                reloadGroup: function () {
                    window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                },
                searchOutUser: function () {
                    $this.outDataGrid.reload({
                        search: {
                            vague: xhExtra.getValue($('#list_out_vague'))
                        },
                        company: xhExtra.getValue($('#bt_list_out_company'))
                    });
                }
            }
        }
    }
}